#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "gamescene.h"
#include <QGraphicsView>

/**
 * @brief MainWindow 构造函数
 * @param parent 父窗口部件的指针
 */
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setFixedSize(820, 620);

    // 1. 创建游戏场景实例
    scene = new GameScene(this);

    // 2. **先**为场景设置正确的尺寸
    scene->setSceneRect(0, 0, 800, 600);

    // 3. 将视图与场景关联
    ui->gameview->setScene(scene);
    ui->gameview->setRenderHint(QPainter::Antialiasing);
    ui->gameview->setCacheMode(QGraphicsView::CacheBackground);
    ui->gameview->setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
    ui->gameview->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->gameview->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // 4. **最后**，在场景尺寸和视图都准备好后，才调用初始化函数
    scene->initScene();
}

/**
 * @brief MainWindow 析构函数
 */
MainWindow::~MainWindow()
{
    delete ui;
}
